package com.fram.service.admin.impl;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.admin.projectplanttechnology.request.ProjectPlantTechnologyAdminSearchRequest;
import com.fram.bean.admin.projectplanttechnology.response.ProjectPlantTechnologyAdminSearchResponse;
import com.fram.bean.sys.PageBean;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.util.qiniu.QiniuUtils;
import com.fram.entity.ProjectPlantTechnology;
import com.fram.mapper.ProjectPlantTechnologyMapper;
import com.fram.service.admin.ProjectPlantTechnologyAdminService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * @作者： 陳曉鬆 @时间： 2020年10月15日 - 上午10:18:11 @description：->
 */
@Service("projectPlantTechnologyAdminService")
public class ProjectPlantTechnologyAdminServiceImpl implements ProjectPlantTechnologyAdminService {

	@Autowired
	private ProjectPlantTechnologyMapper projectPlantTechnologyMapper;
	@Autowired
	private QiniuUtils qiniuUtils;

	@Override
	public PageInfo<ProjectPlantTechnologyAdminSearchResponse> search(PageBean<ProjectPlantTechnologyAdminSearchRequest> request) throws ProAffairException {
		PageHelper.startPage(request.getPageNum(), request.getPageSize());
		List<ProjectPlantTechnologyAdminSearchResponse> list = projectPlantTechnologyMapper.seachAdmin(request.getParam());
		if (list == null || list.size() == 0) {
			throw new ProRunTimeException(ErrCode.NO_DATA.getReturnCode(), ErrCode.NO_DATA.getReturnMsg());
		}
		return new PageInfo<>(list);
	}

	@Override
	@Transactional
	public void add(ProjectPlantTechnology request) throws ProAffairException {
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getUserId() + "/"));
		if (projectPlantTechnologyMapper.insertSelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}

	@Override
	@Transactional
	public void update(ProjectPlantTechnology request) throws ProAffairException {
		if (!StringUtils.isEmpty(request.getImg())) {
			request.setImg(qiniuUtils.uploadImageUrl(request.getImg()));
		}
		request.setIntroduce(qiniuUtils.dealHtml(request.getIntroduce(), "1/" + request.getUserId() + "/"));
		if (projectPlantTechnologyMapper.updateByPrimaryKeySelective(request) <= 0) {
			throw new ProRunTimeException(ErrCode.FAIL.getReturnCode(), ErrCode.FAIL.getReturnMsg());
		}
	}
}
